﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;

namespace good1.Web.Helpers
{
     public static class EnumHelper
    {
         public static SelectList ToSelectList<TEnum>(this TEnum enumObj) where TEnum : struct 
         {
             var values = from TEnum e in Enum.GetValues(typeof(TEnum))
                          select new { Id = (int)(ValueType)e, Name = e.ToString() };
             values  = values.OrderBy(v => v.Name);

             return new SelectList(values, "Id", "Name", enumObj);
         }

         public static SelectList ToSelectListWithoutNone<TEnum>(this TEnum enumObj) where TEnum : struct
         {
             var values = from TEnum e in Enum.GetValues(typeof(TEnum))
                          where (int)(ValueType)e != 0
                          select new { Id = (int)(ValueType)e, Name = e.ToString() };

             values = values.OrderBy(v => v.Name);

             return new SelectList(values, "Id", "Name", enumObj);
         }

     
         public static class EnumUtil
         {
             public static IEnumerable<T> GetValues<T>()
             {
                 return Enum.GetValues(typeof(T)).Cast<T>();
             }
         }
       
    }
}
